package com.mes.schedule.dao;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.common.exception.db.BaseDBException;
import com.mes.common.util.Page;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.mes.schedule.dao.iface.IScheduleSchemeDao;
import com.mes.schedule.domain.ScheduleScheme;

@Repository
public class ScheduleSchemeDao extends BasicDao<ScheduleScheme> implements IScheduleSchemeDao {
	private static final Log log = LogFactory.getLog(ScheduleSchemeDao.class);

	/**
	 * @see the method use for ScheduleManager
	 * @param scheme
	 */
	public void save(ScheduleScheme scheme) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * @author hba
	 * @see the method use for ScehduelManager
	 * @param deptId
	 * @param schemeType
	 * @return
	 */
	public List<ScheduleScheme> findSchemeByDeptIdAndType(String deptId, String schemeType) {
		// TODO Auto-generated method stub
		List<ScheduleScheme> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(ScheduleScheme.class);
			crit.add(Restrictions.eq("deptUid", deptId));
			if ("".equals(schemeType) || schemeType == null) {

			} else {
				crit.add(Restrictions.eq("schemeType", schemeType));
			}
			crit.addOrder(Property.forName("createTime").desc());
			list = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @see the method use for ScheduleManager
	 * @param baseScheduleUid
	 * @return
	 */
	public List<ScheduleScheme> findSchemeByBaseScheduleUid(
			String baseScheduleUid) {
		List<ScheduleScheme> ScheduleList = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(ScheduleScheme.class);
			crit.add(Restrictions.eq("baseScheduleUid", baseScheduleUid));
			crit.addOrder(Property.forName("createTime").desc());
			ScheduleList = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ScheduleList;
	}
	 
	/**
	 * @see the method use for ScheduelSchemeManager
	 * @param deptId
	 * @return
	 * @throws BaseDBException
	 */
	public List<ScheduleScheme> getCapcitySchemes(String deptId)
			throws BaseDBException {
		List list = null;
		try {
			Session session = this.getSession();
			StringBuilder hql = new StringBuilder();
			hql.append(" from " +ScheduleScheme.class.getName()+ " where ");
			hql.append("  scheduleType=6 ");
			hql.append("  and ? like deptId || '%' ");
			Query query = session.createQuery(hql.toString());
			query.setString(0, deptId);
			list = query.list();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	 /**
	  * @see the method use for ScheduelSchemeManager
	  * @return
	  * @throws BaseDBException
	  */
	public List<ScheduleScheme> getCapcitySchemes() throws BaseDBException {
		List list = null;
		try {
			Session session = this.getSession();
			StringBuilder hql = new StringBuilder();
			hql.append(" from " + ScheduleScheme.class.getName() + " where ");
			hql.append("  scheduleType=6 ");
			Query query = session.createQuery(hql.toString());
			list = query.list();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @see the method use for ScheduleSchemeManager
	 * @param deptId
	 * @param page
	 * @return
	 * @throws BaseDBException
	 */
	public List<ScheduleScheme> getSchemesByDeptId(String deptId, Page page)
			throws BaseDBException {
		List<ScheduleScheme> ScheduleList = null;

		try {

			Session session = this.getSession();
			Criteria crit = session.createCriteria(ScheduleScheme.class);

			crit.add(Restrictions.eq("deptUid", deptId));

			// 如果需要进行分页处理，则调用如下语句
			if (page != null)
				page.pagePro(crit);
			crit.addOrder(Property.forName("createTime").desc());
			ScheduleList = crit.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ScheduleList;

	}
	
	/**
	 * @see the method use for ScheduleSchemeManager
	 * @param deptId
	 * @return
	 * @throws BaseDBException
	 */
	public List<ScheduleScheme> getSchemesByDeptId(String deptId)
			throws BaseDBException {
		List list = null;
		try {
			Session session = this.getSession();
			StringBuilder hql = new StringBuilder();
			hql.append(" from " + ScheduleScheme.class.getName() + " where ");
			hql.append("? = deptId ");
			Query query = session.createQuery(hql.toString());
			query.setString(0, deptId);
			list = query.list();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
